var token = localStorage.getItem("key")
var loginData = JSON.parse(localStorage.getItem("LoginData"))
var like = loginData.like
var myLike = [];

function likeInit(){
	if(like!=""){
		var likes = []
		likes = like
		for(var i=0;i<likes.length;i++){
			$("p").each(function(index){
				if($(this).html() == likes[i]){
					this.style.color = "white"
					this.style.backgroundColor = "#B12F41"
				}
			})
		}
	}
}

function clickLike(ele){
	if(ele.style.color != ""){
		ele.style.color = ""
		ele.style.backgroundColor = ""
	}else{
		ele.style.color = "white"
		ele.style.backgroundColor = "#B12F41"
	}
}


function backIndex(){
	//var newLike = ""
	var sum = $("p[style='color: white; background-color: rgb(177, 47, 65);']").length
	var newLike = []
	$("p[style='color: white; background-color: rgb(177, 47, 65);']").each(function(i){
//		if(i!=sum-1){
//			newLike += $(this).html() +","
//		}else{
//			newLike += $(this).html()
//		}
		newLike[i] = $(this).html()
	})
//	newLike = "[" + newLike + "]"
	if(newLike.toString()!=like.toString()){
		upLike(newLike)	
	}else{
		window.location.href='home.html?bool=true'
	}
}

function upLike(like) {
	$.ajax({
        type: "POST",
        url: url_prefix + "user/update_likes",
        data: {
        	request: JSON.stringify({
				likes: "[" + like.toString() + "]",
				userId: loginData.id,
				token: token
			})   
        },
        dataType: "json",
        success: function(data){
        	//plus.nativeUI.toast(data.message, {verticalAlign: 'center'});
        	if (data.code == '300'){
				mui.toast("帐号在其他设备上登录")
				window.location.href="fastLogin.html"
			} else if (data.code == '200'){
				loginData.like = like
				localStorage.setItem('LoginData', JSON.stringify(loginData));
				window.location.href='home.html?bool=true'
			} else {
				mui.toast(data.message)
			}
        }
    });
}